package controller.manufacture;

import java.sql.SQLException;

import service.CoffeeService;
import to.JobTO;
import util.CoffeeUtil;

public class Pressure extends Thread {
	private static CoffeeService service;

	public Pressure(CoffeeService service) {
		this.service = service;
	}

	@Override
	public void run() {
		while (true) {
			try {
				int job = service.selectMachineWaitingJob("프레셔");
				while (job > 0) {
					System.out.println("프레셔job = " + job);
					try {
						// 대기작업 -1 시킨다.
						// 프레셔 상태 변환 - 작업중
						service.updateMachineWaitingJob("프레셔", -1);
						service.updateMachineStatus("프레셔", "작업중");
					} catch (SQLException e) {
						e.printStackTrace();
					}
					String startTime = CoffeeUtil.getNow();
					CoffeeUtil.sleep(CoffeeUtil.PRESSING_TIME);
					// 용액 제조루틴
					int resultWeight = (100 / 2) + 1000; // 가루 100KG + 물 1000L
															// 결과 L
					String endTime = CoffeeUtil.getNow();
					JobTO jto = new JobTO("프레싱", startTime, endTime);
					jto.setWorkLoad(resultWeight);
					service.insertJobInfo(jto);
					// 프레셔 상태변환 - 대기중

					// 혼합기 대기작업+1 시킨다
					try {
						service.updateMaterialTotalWeight("원액", resultWeight);
						service.updateMachineStatus("프레셔", "대기중");
						service.updateMachineWaitingJob("혼합기", 1);
					} catch (SQLException e) {
						e.printStackTrace();
					}
					job = service.selectMachineWaitingJob("프레셔");
				}
			} catch (SQLException e) {
				e.printStackTrace();

			}
		}
	}

	static public void clean() {
		// 프레셔 상태값 변경 - 청소
		try {
			String startTime = CoffeeUtil.getNow();
			service.updateMachineStatus("프레셔", "청소중");
			CoffeeUtil.sleep(CoffeeUtil.PRESSURE_CLEAN_TIME);// 청소시간 2분
			String endTime = CoffeeUtil.getNow();
			JobTO jto = new JobTO("프레셔 청소", startTime, endTime);
			service.insertJobInfo(jto);
			service.updateMachineStatus("프레셔", "대기중");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
